﻿/*
* Copyright (C) 2020 ~ 2024 chaigec All Rights Reserved.
*
* Author:     chaigec <chaigec@163.com>
* Maintainer: chaigec <chaigec@163.com>

 * Use of this source code is governed by MIT license that can be found in the
 * LICENSE file in the root of the source tree. All contributing project authors
 * may be found in the AUTHORS file in the root of the source tree.
*/

#ifndef MK_PS_PARSER_H
#define MK_PS_PARSER_H
#include "MkMedia/MkMediaUtil.h"

class CMkVideoParam;
class CMkAudioParam;
class MkApi CMkPsDemuxer
{
public:
    CMkPsDemuxer();
    ~CMkPsDemuxer();

    Uint32 Input(Uint8 *pBuf, Uint32 nLen, Uint32& nUsed, BOOL bEnd = FALSE);
    Uint32 InputPsPacket(Uint8 *pPs, Uint32 nLen);
    const CMkVideoParam& GetVideoParam() const { return m_VideoParam; }
    const CMkAudioParam& GetAudioParam() const { return m_AudioParam; }
    Uint32 GetEsPacket(MkEsPacket &Packet);
    static Uint32 GetPsHeader(Uint8 *pBuf, Uint32 nLen, Uint8 **psHeader);
    static Uint32 GetPsSubItem(Uint8 *pBuf, Uint32 nLen, Uint8 **ppHeader);
private:

    Uint32 ParsePsHeader(Uint8* pBuf, Uint32 nLen, Uint32& nUsed);
    Uint32 ParseSystemHeader(Uint8* pBuf, Uint32 nLen, Uint32& nUsed);
    Uint32 ParsePsMap(Uint8* pBuf, Uint32 nLen, Uint32& nUsed);
    Uint32 ParsePes(Uint8 *pBuf, Uint32 nLen, const Uint32& PacketStartCode, Uint32& nUsed);



    //Uint32 InputFrameQueue();
private:
    MkQueue<MkEsPacket> m_EsQueue;
    CMkVideoParam      m_VideoParam;
    CMkAudioParam      m_AudioParam;
    MkMap<Uint32, Uint8> m_MapPsEsType;

    //Uint64              m_LastPts;
    //Uint64              m_LastDts;
    MkEsPacket            m_VideoPacket;
    Uint32                m_LastVideoPts;
    Uint8*                m_pLastEsBegin;
    Uint32                m_nLastEsLen;

    BOOL                  m_DecodeAacAdts;
};









#endif


